<template>
  <el-dialog
    title="新增"
    :visible.sync="dialogFormVisible"
    @close="closeDialog"
  >
    <el-form :model="form">
      <el-form-item label="类型">
        <el-select v-model="form.category" placeholder="请选择类型">
          <template v-for="c in category">
            <el-option
              :key="c.id"
              :label="c.category_name"
              :value="c.id"
            ></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容">
        <el-input type="textarea" v-model="form.content"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addInfo } from "@/api/info";
export default {
  name: "InfoDialog",
  props: {
    flag: {
      type: Boolean,
      default: false
    },
    category: {
      type: Array,
      request: true
    },
    module: {
      type: String,
      default: "create"
    },
    item: {
      type: Object,
      default: undefined
    }
  },
  watch: {
    flag(newValue) {
      this.dialogFormVisible = newValue;
    },
    item(newValue) {
      if (newValue){
        this.form.title = newValue.title;
        this.form.category = newValue.categoryId;
        this.form.content = newValue.content;
      }else{
        this.form.title = "";
        this.form.category = "";
        this.form.content = "";
      }
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {
        title: "",
        category: "",
        content: ""
      }
    };
  },
  methods: {
    submit() {
      if (this.module == "create") {
        addInfo(this.form).then(res => {
          this.$message.success(res.message);
          this.dialogFormVisible = false;
        });
      }
    },
    closeDialog() {
      this.$emit("update:flag", false);
    }
  }
};
</script>

<style scoped></style>
